;
! function() {
	var layer = layui.layer,
		form = layui.form,
		laypage = layui.laypage;

//	var status = null;
	
	layer.load(2);

	to_page(1, 10);

	// 请求分页数据
	function to_page(pageNum, pageSize) {
		$.ajax({
			url: "/bizjobsorder/",
			type: "post",
			data: {
				"pageNum": pageNum,
				"pageSize": pageSize,
//				"status":status
			},
			async: false,
			success: function(result) {
				layer.closeAll('loading');
				if(result.totalRow == 0) {
					// 提示没有数据
					$("#content").html("<tr><td colspan='7' style='text-align:center;font-size: 18px;color:#FF6700;'>没有查询到任何数据哦~</td></tr>");
					// 隐藏分页条
					$("#page_info").hide();
				} else {
					// 拼装tr并赋给页面
					build_table(result);
					// 拼装分页总览信息并赋给页面
					build_pageInfo(result);
					// 显示分页按钮
					laypage.render({
						elem: 'page_nav_div',
						count: result.totalRow,
						curr: result.pageNumber,
						limit: result.pageSize,
						theme: "#FF6700",
						prev: "<em>«</em>",
						next: "<em>»</em>",
						layout: ['prev', 'page', 'next', 'limit', 'skip'],
						jump: function(obj, first) {
							if(!first) {
								to_page(obj.curr, obj.limit);
							}
						}
					});
					// 显示分页条
					$("#page_info").show();
				}
			}
		});
	}

	// 构建分页细节
	function build_pageInfo(pageInfo) {
		$("#pagination_overview").html(
			"当前第" + pageInfo.pageNumber + "页," +
			"共有" + pageInfo.totalPage +
			"页,总计" +
			pageInfo.totalRow + "条记录");
	}

	// 构建表格
	function build_table(page) {
		// 清空tbody
		$("#content").empty();
		// 遍历数据并累加
		$.each(page.list, function() {
			var title = this.columns.title;
			if(title.length > 12){
				title = title.substring(0,11)+"...";
			}
			var titleTd = $("<td></td>").append(title);
			var typeNameTd = $("<td></td>").append(this.columns.name);
			var priceTd = $("<td></td>").append(this.columns.price).append("&nbsp;RMB");
			var dateTd = $("<td></td>").append(datedifference(getMyDate((new Date()).valueOf()),getMyDate(this.columns.createTime))+"天");
			var operation = $("<td>" +
				"<button id='"+this.columns.id+"' class='layui-btn layui-btn-radius layui-btn-sm layui-btn-normal bizjobs-info'>" +
				"<i class='layui-icon'>&#xe615;</i>查看任务信息</button>" +
				"<button id='"+this.columns.publishUserId+"' class='layui-btn layui-btn-radius layui-btn-sm layui-btn-normal secuser'>" +
				"<i class='layui-icon'>&#xe615;</i>查看推广人信息</button>" +
				"<button id='"+this.columns.id+"' class='layui-btn layui-btn-radius layui-btn-sm layui-btn-normal appuser'>" +
				"<i class='layui-icon'>&#xe615;</i>查看接单人信息</button>" +
				"</td>");
			// 赋给tbody
			$("<tr></tr>").append(titleTd).append(typeNameTd).append(priceTd).append(dateTd).append(operation).appendTo("#content");
		});
	}

	$(document).on("click", ".bizjobs-info", function() {
		var id = $(this).attr("id");
		location.href = "/bizJobsInfo/" + id;
	});
		$(document).on("click", ".secuser", function() {
		var id = $(this).attr("id");
		location.href = "/secUserInfo/" + id;
	});
	

	$(document).on("click", ".appuser", function() {
		var id = $(this).attr("id");
		$.ajax({
			url: "/bizjobsorder/getAppUserByBizJobsId",
			type: "post",
			data: {
				"id": id
			},
			success: function(result) {
				if(result.status == 0) {
					$("#content-order").empty();
					$.each(result.data, function() {
						var realNameTd =  $("<td></td>").append(this.columns.realName);
						var operation = $("<td>" +
							"<button id='"+this.columns.userId+"' class='layui-btn layui-btn-radius layui-btn-sm layui-btn-normal userId'>" +
							"<i class='layui-icon'>&#xe615;</i>查看接单人信息</button>" +
							"<button id='"+this.columns.id+"' class='layui-btn layui-btn-radius layui-btn-sm layui-btn-normal bizJobsId'>" +
							"<i class='layui-icon'>&#xe615;</i>查看审核任务信息</button>" +
							"</td>");
						// 赋给tbody
						$("<tr></tr>").append(realNameTd).append(operation).appendTo("#content-order");
					});
					
					$(".userId").click(function(){
						var id = $(this).attr("id");
						location.href="/appUserInfo/"+id;
					});
					
					$(".bizJobsId").click(function(){
						var id = $(this).attr("id");
						location.href="/lookBizJobsSubmitResult/"+id;
					});
					
					$("#userInfo").show();
				}
			}
		});
	});
	
	
	
    $("#userInfo").click(function(){
    	$("#userInfo").hide();
    });
    
    $("#userInfo *").click(function(e){
    	stopPropagation(e);
    })
	
//	form.on('select(auditStatus)', function(data) {
//		status = data.value;
//		to_page(1, 10);
//	});
	
	function datedifference(sDate1, sDate2) {    //sDate1和sDate2是2006-12-18格式  
        var dateSpan,
            tempDate,
            iDays;
        sDate1 = Date.parse(sDate1);
        sDate2 = Date.parse(sDate2);
        dateSpan = sDate2 - sDate1;
        dateSpan = Math.abs(dateSpan);
        iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
        return iDays
    };

	function getMyDate(str) {
		var oDate = new Date(str),
			oYear = oDate.getFullYear(),
			oMonth = oDate.getMonth() + 1,
			oDay = oDate.getDate(),
			oHour = oDate.getHours(),
			oMin = oDate.getMinutes(),
			oSen = oDate.getSeconds(),
			oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen); //最后拼接时间  
		return oTime;
	};
	//补0操作
	function getzf(num) {
		if(parseInt(num) < 10) {
			num = '0' + num;
		}
		return num;
	}
	
	/*阻止事件冒泡*/
    function stopPropagation(e) {
        e = e || window.event;
        if (e.stopPropagation) { //W3C阻止冒泡方法
            e.stopPropagation();
        } else {
            e.cancelBubble = true; //IE阻止冒泡方法
        }
    }
}();